#ifndef NRF24
#define NRF24

#include "nRF24L01.h"
#include <stdint.h>

// 速率设置
enum SPEED
{
	SPEED_250Kbps = 0x20, // 0010 0000
	SPEED_1Mbps = 0x00,   // 0000 0000
	SPEED_2Mbps = 0x08    // 0000 1000
};
// 功率设置 N表示负数
enum POWER
{
	POWER_7dBm = 0x07,    // 0000 0111
	POWER_4dBm = 0x06,    // 0000 0110
	POWER_3dBm = 0x05,    // 0000 0101
	POWER_1dBm = 0x04,    // 0000 0100
	POWER_0dBm = 0x03,    // 0000 0011
	POWER_N4dBm = 0x02,   // 0000 0010
	POWER_N6dBm = 0x01,   // 0000 0001
	POWER_N12dBm = 0x00   // 0000 0000
};
// 信道设置
enum CHANNEL
{
	CHANNEL_0 = 0x00
};
// 管道设置
enum PIPE
{
	PIPE_A = 0x01
};
// config设置
enum CONFIG_SET
{
	ENABLE_CRC = 0
};
// 状态变量 标识整个系统工作状态

// RF_SETUP 寄存器变量
#define REPORT_SPEED SPEED_1Mbps
#define REPORT_POWER POWER_7dBm
#define REPORT_CHANNEL (CHANNEL_0 + 91)
#define REPORT_SIZE 8



#define SENDER 0
#define SENSORS_USED 1
#define PACKGET_TOTAL 10000
#define PACKGET_RESET_TOTAL 5000
//


// 支持到8个sensor
enum SENSOR{
	SENSOR_CLICK_1 = 1,
	SENSOR_CLICK_2 = 2,
	SENSOR_CLICK_3 = 3,
	SENSOR_CLICK_4 = 4,
	SENSOR_CLICK_5 = 5,
	SENSOR_CLICK_6 = 6,
	SENSOR_CLICK_7 = 7,
	SENSOR_CLICK_8 = 8
};

// CE pin functions
#define FAST_CE_SET_HIGH_SENSOR_CLICK_1() (GPIOA->BSHR = GPIO_Pin_0)
#define FAST_CE_SET_HIGH_SENSOR_CLICK_2() (GPIOA->BSHR = GPIO_Pin_5)

#define FAST_CE_SET_LOW_SENSOR_CLICK_1() (GPIOA->BCR = GPIO_Pin_0)
#define FAST_CE_SET_LOW_SENSOR_CLICK_2() (GPIOA->BCR = GPIO_Pin_5)
//

// CSN pin functions
#define FAST_CSN_SET_HIGH_SENSOR_CLICK_1() (GPIOA->BSHR = GPIO_Pin_15)
#define FAST_CSN_SET_HIGH_SENSOR_CLICK_2() (GPIOA->BSHR = GPIO_Pin_8)

#define FAST_CSN_SET_LOW_SENSOR_CLICK_1() (GPIOA->BCR = GPIO_Pin_15)
#define FAST_CSN_SET_LOW_SENSOR_CLICK_2() (GPIOA->BCR = GPIO_Pin_8)
//


#define LOW 0
#define HIGH 1

#define nrf24_ADDR_LEN 5
#define nrf24_CONFIG ((1<<EN_CRC)|(0<<CRCO))

#define NRF24_TRANSMISSON_OK 0
#define NRF24_MESSAGE_LOST   1

/* adjustment functions */
void    nrf24_init(uint8_t sensor);
void    nrf24_rx_address(uint8_t sensor, uint8_t* adr);
void 	nrf24_rx_address_sensors(uint8_t sensors, uint8_t * adr);
void    nrf24_tx_address(uint8_t sensor, uint8_t* adr);
void 	nrf24_tx_address_sensors(uint8_t sensors, uint8_t * adr);
void    nrf24_config(uint8_t sensor);

/* state check functions */
uint8_t nrf24_dataReady(uint8_t sensor);
uint8_t nrf24_isSending(uint8_t sensor);
uint8_t nrf24_sensorsIsSending(uint8_t sensors);
uint8_t nrf24_getStatus(uint8_t sensor);
uint8_t nrf24_rxFifoEmpty(uint8_t sensor);

/* core TX / RX functions */

void nrf24_send_front(uint8_t sensor, uint8_t* value);
void nrf24_send_back(uint8_t sensor);

void    nrf24_send(uint8_t sensor, uint8_t* value);
void    nrf24_getData(uint8_t sensor, uint8_t* data);

/* use in dynamic length mode */
uint8_t nrf24_payloadLength(uint8_t sensor);

/* post transmission analysis */
uint8_t nrf24_lastMessageStatus(uint8_t sensor);
uint8_t nrf24_retransmissionCount(uint8_t sensor);

/* Returns the payload length */
uint8_t nrf24_payload_length(uint8_t sensor);
void nrf24_init_sensors(uint8_t sensors);
/* power management */
void    nrf24_powerUpRx(uint8_t sensor);
void    nrf24_powerUpTx(uint8_t sensor);
void    nrf24_powerDown(uint8_t sensor);
void 	nrf24_powerDown_sensors(uint8_t sensors);
/* low level interface ... */
uint8_t spi_transfer(uint8_t tx);
void    nrf24_transmitSync(uint8_t* dataout,uint8_t len);
void    nrf24_transferSync(uint8_t* dataout,uint8_t* datain,uint8_t len);
// 涉及到 sensor
void    nrf24_configRegister(uint8_t sensor, uint8_t reg, uint8_t value);
void    nrf24_readRegister(uint8_t sensor, uint8_t reg, uint8_t* value, uint8_t len);
void    nrf24_writeRegister(uint8_t sensor, uint8_t reg, uint8_t* value, uint8_t len);

/* -------------------------------------------------------------------------- */
/* You should implement the platform spesific functions in your code */
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
/* In this function you should do the following things:
 *    - Set MISO pin input
 *    - Set MOSI pin output
 *    - Set SCK pin output
 *    - Set CSN pin output
 *    - Set CE pin output     */
/* -------------------------------------------------------------------------- */
extern void nrf24_setupPins(uint8_t sensor);

/* -------------------------------------------------------------------------- */
/* nrf24 CE pin control function
 *    - state:1 => Pin HIGH
 *    - state:0 => Pin LOW     */
/* -------------------------------------------------------------------------- */
extern void nrf24_ce_digitalWrite(uint8_t sensor, uint8_t state);

/* -------------------------------------------------------------------------- */
/* nrf24 CE pin control function
 *    - state:1 => Pin HIGH
 *    - state:0 => Pin LOW     */
/* -------------------------------------------------------------------------- */
extern void nrf24_csn_digitalWrite(uint8_t sensor, uint8_t state);

/* -------------------------------------------------------------------------- */
/* nrf24 SCK pin control function
 *    - state:1 => Pin HIGH
 *    - state:0 => Pin LOW     */
/* -------------------------------------------------------------------------- */
extern void nrf24_sck_digitalWrite(uint8_t state);

/* -------------------------------------------------------------------------- */
/* nrf24 MOSI pin control function
 *    - state:1 => Pin HIGH
 *    - state:0 => Pin LOW     */
/* -------------------------------------------------------------------------- */
extern void nrf24_mosi_digitalWrite(uint8_t state);

/* -------------------------------------------------------------------------- */
/* nrf24 MISO pin read function */
/* - returns: Non-zero if the pin is high */
/* -------------------------------------------------------------------------- */
extern uint8_t nrf24_miso_digitalRead();
//#define SOFT_SPI

#endif
